﻿using System;
using System.Net.Mail;
using System.Xml.Xsl;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
using System.Text;
using System.Web;
namespace Dissemination.MVC.Helpers
{
    public class MailSender
    {
        public static bool SendEmail(string subject, string templateName, IDictionary parameters, bool isBodyHtml)
        {
            try
            {
                return SendEmail("david.martos@gmail.com", subject + " (Administration email)", templateName, parameters, isBodyHtml);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool SendEmail(string to, string subject, string templateName, IDictionary parameters, bool isBodyHtml, bool makeCCC)
        {
            try
            {
                var cc = ConfigurationHelper.GetConfigurationValue("SMTPEmailCCC");
                SendEmail(to, subject, templateName, parameters, isBodyHtml);
                SendEmail(subject, templateName, parameters, isBodyHtml);
                return SendEmail(cc, subject + " (Security email)", templateName, parameters, isBodyHtml);
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public static bool SendEmail(string to, string subject, string templateName, IDictionary parameters, bool isBodyHtml)
        {

            try
            {
                var mail = new MailMessage();
                var smtp = new SmtpClient();

                mail.From = new MailAddress(ConfigurationHelper.GetConfigurationValue("SMTPFromEmail"), ConfigurationHelper.GetConfigurationValue("SMTPFromName"));
                mail.To.Add(to);
                mail.Subject = subject;
                mail.Body = CreateBody(templateName, parameters);
                mail.IsBodyHtml = isBodyHtml;
                smtp.Host = ConfigurationHelper.GetConfigurationValue("SMTPUrl");
                smtp.Port = int.Parse(ConfigurationHelper.GetConfigurationValue("SMTPPort"));
                smtp.UseDefaultCredentials = false;
                smtp.Credentials = new System.Net.NetworkCredential(ConfigurationHelper.GetConfigurationValue("SMTPFromEmail"), ConfigurationHelper.GetConfigurationValue("SMTPFromPassword"));
                smtp.Send(mail);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private static string CreateBody(string xslttemplatename, IDictionary objDictionary)
        {
            string templatepath = HttpContext.Current.Server.MapPath("~/Views/Shared/MailTemplates");

            XslTransform objxslt = new XslTransform();

            objxslt.Load(System.IO.Path.Combine(templatepath, xslttemplatename));


            XmlDocument xmldoc = new XmlDocument();
            xmldoc.AppendChild(xmldoc.CreateElement("DocumentRoot"));
            XPathNavigator xpathnav = xmldoc.CreateNavigator();


            XsltArgumentList xslarg = new XsltArgumentList();
            if (objDictionary != null)
                foreach (DictionaryEntry entry in objDictionary)
                {
                    xslarg.AddExtensionObject(entry.Key.ToString(), entry.Value);
                }

            StringBuilder emailbuilder = new StringBuilder();
            XmlTextWriter xmlwriter = new XmlTextWriter(new System.IO.StringWriter(emailbuilder));

            objxslt.Transform(xpathnav, xslarg, xmlwriter, null);

            string subjecttext, bodytext;

            XmlDocument xemaildoc = new XmlDocument();
            xemaildoc.LoadXml(emailbuilder.ToString());
            XmlNode titlenode = xemaildoc.SelectSingleNode("//title");

            subjecttext = titlenode.InnerText;

            XmlNode bodynode = xemaildoc.SelectSingleNode("//body");

            bodytext = bodynode.InnerXml;
            if (bodytext.Length > 0)
            {
                bodytext = bodytext.Replace("&amp;", "&");
            }

            return bodytext;

        }
    }
}